#pragma once
#include <cstdint>
#include <functional>
#include <memory>
#include <map>
#include <list>
#include <unordered_map>
#include <atomic>
class Timer
{
public:
    /**
    * \tickMs 时间轮精度
    * \wheelSize      时间轮长度
    */
    Timer(uint32_t tickMs, uint32_t wheelSize);
    ~Timer();

    // 创建了，定时器就启动了
    uint32_t Creat(uint32_t ms, std::function<void(uint32_t id, std::shared_ptr<void>)>, std::shared_ptr<void> userdata);

    // 停止定时器
    void StopOne(uint32_t id);

    void StopAll();

    void onTimer();

protected:
    struct TimerNode {
        uint32_t id;
        bool repeat;
        bool cancel;
        uint32_t interval;
        uint32_t circle;
        std::shared_ptr<void> userdata;
        std::function<void(uint32_t id, std::shared_ptr<void >)> callback;
    };

    void  checkTimer(uint32_t i);

private:
    uint32_t m_tickMs{ 1 };
    uint32_t m_wheelSize{ 512 };

    std::vector<std::list<std::shared_ptr<TimerNode>>> TimerNodeVec;
    std::atomic<uint32_t> ticks{ 0 };
};
